const dotenv = require('dotenv');
dotenv.config(); // 加载环境变量
const qiniu = require('qiniu')

//七牛云配置信息
const accessKey = process.env.QINIU_ACCESS_KEY;
const secretKey = process.env.QINIU_SECRET_KEY;
const bucket = process.env.QINIU_BUCKET;

// 生成上传凭证
function getUploadToken(folder) {
  const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
  const options = {
    scope: `${bucket}:${folder}`, // 动态指定存储路径
    expires: 7200 // 有效期2小时
  }
  const putPolicy = new qiniu.rs.PutPolicy(options)
  return putPolicy.uploadToken(mac)
}

module.exports = { getUploadToken }